package com.xiaohu.mayi_huiyuan.service;

import com.xiaohu.mayi_huiyuan.entity.Customer;
import com.xiaohu.mayi_huiyuan.mapper.CustomerMapper;
import com.xiaohu.mayi_huiyuan.mapper.YuebiaoMapper;
import com.xiaohu.mayi_huiyuan.model.CustomerDto;
import com.xiaohu.mayi_huiyuan.model.HuiZong;
import com.xiaohu.mayi_huiyuan.model.YueBiaoDto;
import com.xiaohu.mayi_huiyuan.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class QueryService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private YuebiaoMapper yuebiaoMapper;

    @Autowired
    RegisterAndConsumption registerAndConsumption;


    /**
     * 获取所有用户
     *
     * @return
     */
    public List<CustomerDto> getCustomerList() {
        List<CustomerDto> list = customerMapper.getAllCustomer();
        list.forEach(customer -> {
            customer.setFormatDate(DateUtil.formatDate(customer.getCreatedate()));
            Double chongzhiyue=registerAndConsumption.getCustomerChongZhiYue(customer.getId());
            if(chongzhiyue==null){
                customer.setChongZhiYue(0.0);
            }else{
                customer.setChongZhiYue(chongzhiyue);
            }
            Double fanxianyue=registerAndConsumption.getFanXianYue(customer.getId());
            if(fanxianyue==null){
                customer.setFanXianYue(0.0);
            }else{
                customer.setFanXianYue(fanxianyue);
            }

        });

        return list;
    }

    /**
     * 获取所有 余额表 明细
     *
     * @return
     */
    public List<YueBiaoDto> getAllYue() {

        List<YueBiaoDto> list = yuebiaoMapper.getAllYueBiao();
        list.forEach(yueBiaoDto -> {
            yueBiaoDto.setFormatDate(DateUtil.formatDate(yueBiaoDto.getCreatedate()));
            ;
        });
        return list;
    }

    /**
     * 汇总
     *
     * @return
     */
    public HuiZong getHuiZong() throws Exception {
        Class clazz = Class.forName("com.xiaohu.mayi_huiyuan.model.HuiZong");
        HuiZong huiZong = (HuiZong) clazz.newInstance();
        huiZong.setCount(customerMapper.getCustomerCount());
        huiZong.setChongzhiAll(yuebiaoMapper.getChongZhieSum());
        huiZong.setChongZhiXiaoFeiAll(yuebiaoMapper.getChongZhiXiaoFei());
        huiZong.setFianXianAll(yuebiaoMapper.getFanXianAll());
        huiZong.setDate(DateUtil.formatDate(new Date()));
        return huiZong;
    }

}
